# -*- coding: utf-8 -*- 
from calculator import Calculator
import utility

class Calculator_crab_back(Calculator):
    def sort_dots(self):
        r = []
        ds = utility.sort_dots(self.dots.get_dots(), 'y')
        
        d7 = ds[:7];ds = ds[7:]
        #print d7
        d7 = utility.sort_dots(d7, 'x')
        num = [3,2,4,1,5,0,6]
        
        for i in num:
            r.append(d7[i])
        
        d6 = ds[:8];ds = ds[8:]
        num = [0,2,4,6]
        for i in num:
            a = d6[i];b = d6[i+1]
            r.append(min(a,b))
            r.append(max(a,b))
        
        d3 = ds[:]    
        d3 = utility.sort_dots(d3, 'x')
        r.append(d3[0])
        r.append(d3[2])
        r.append(d3[1])    
        
        self.dots.set_dots(r)
        
    
    def prepare_raw_data(self):
        
        two_dots = [('A1',13,14), ('A2',11,12),('A3',9,10),('A4',7,8),
            ('A5',5,6),('A6',3,4),('A7',1,2),('A8',1,3),('A9',15,16),('A9',0,17)]
        
        self.sort_dots()
        ds = self.dots.get_dots()
        head = [] ; r = []
        for d in two_dots:
            s = "%s(%s-%s)"%(d[0],d[1]+1,d[2]+1)
            head.append(s)
            
            a, b = ds[d[1]],ds[d[2]]
            d = utility.distanceByPixel(a,b)
            r.append(d)
        
        self.result.append([head,])
        self.result.append(r)
            
    def prepare_title(self):
        self.title += "河蟹头胸甲背部形态参数"

class Calculator_crab_belly(Calculator):
    
    def sort_dots(self):
        r = []
        ds = utility.sort_dots(self.dots.get_dots(), 'y')
        
        r.append(ds[0])
        num=[1,3,5,7]
        for i in num:
            a = ds[i];b = ds[i+1]
            r.append(min(a,b))
            r.append(max(a,b))
        r.append(ds[-1])    
        
        self.dots.set_dots(r)
        
    def prepare_raw_data(self):
        
        two_dots = [('D1',1,2), ('D2',3,4),('D3',5,6),('D4',7,8),('M',0,9)]
        
        self.sort_dots()
        ds = self.dots.get_dots()
        head = [] ; r = []
        for d in two_dots:
            s = "%s(%s-%s)"%(d[0],d[1],d[2])
            head.append(s)
            
            a, b = ds[d[1]],ds[d[2]]
            d = utility.distanceByPixel(a,b)
            r.append(d)
        
        self.result.append([head,])
        self.result.append(r)
            
    def prepare_title(self):
        self.title += "河蟹头腹甲形态参数"
        
